﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Nature.Data
{
    public static class CkUtil
    {
        public static IEnumerable<IMixtureDefinition>
            GetMixtureDefinitions(string componentName, string expressionString, System.Collections.IEnumerable entitiesHeap)
        {
            var adapter = new GMixMarkup.GMixExpressionAdapter(expressionString, entitiesHeap);
            return adapter[componentName];
        }



        public static IdealGasMixture CreateIdealGasMixture(string mixtureExpression,
            params Uri[] resourceUri)
        {
            var model = CkUtil.LoadModel(resourceUri);
            return model.CreateIdealGasMixture(mixtureExpression);
        }

        public static IdealGasMixture CreateIdealGasMixture(string mixtureExpression,
            params string[] resourceUri)
        {

            var model = CkUtil.LoadModel(resourceUri);
            return model.CreateIdealGasMixture(mixtureExpression);
        }

        public static ModelObject LoadModel(ModelSetupInfo setupInfo, params Uri[] resourceUri)
        {
            return new Nature.Data.AH2004ModelObject(setupInfo, resourceUri);
        }

        public static ModelObject LoadModel(params Uri[] resourceUri)
        {
            return CkUtil.LoadModel(new ModelSetupInfo(), resourceUri);
        }

        public static ModelObject LoadModel(params string[] path) 
        {
            return CkUtil.LoadModel(new ModelSetupInfo(), path);
        }

        public static ModelObject LoadModel(ModelSetupInfo setupInfo, params string[] path)
        {
            return CkUtil.LoadModel(
                    setupInfo,
                    (from p in path
                    let uri = Uri.IsWellFormedUriString(p, UriKind.Absolute) 
                        ? new Uri(p)
                        : new Uri((new FileInfo(p)).FullName)
                    select uri).ToArray()
                );
        }

        public static Thermo7 BuildThermo7FromCp(string speciesID, ThermoRef @ref, IEnumerable<double> temperatures, Func<double, double> cpPlot)
        {
            var approximation = Thermo7Approximation.FromCpValues(@ref, temperatures, cpPlot);
            Thermo7 th7 = new Thermo7() 
            {
                SpeciesID = speciesID,
                Ranges = approximation.ToArray()
            };
            return th7;
        }

        public static string ToString(Thermo7 th7)
        {
            throw new NotImplementedException();
        }
    }
}
